/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld;

import com.sun.spot.io.j2me.radiogram.RadiogramConnection;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;

/**
 *
 * @author seb
 */
public class BroadcastReceiveThread extends Thread{

    private Main main;
    private RadiogramConnection radiogramConnection;

    public BroadcastReceiveThread(Main main){
        try {
            radiogramConnection = (RadiogramConnection) Connector.open("radiogram://:37");
            //System.out.println("RadioCon opend");

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.main = main;
    }
    
    public void run() {
        while(true) {
            try {
                Datagram datagram = radiogramConnection.newDatagram(radiogramConnection.getMaximumLength());
                radiogramConnection.receive(datagram);
                main.recieveBroadcastMessage(datagram, radiogramConnection);
            } catch(IOException ioe)
            {
                System.err.println("Error @ BroadcastRecieveThread:run - Connection not available");
                ioe.printStackTrace();
            }
        }
        
    }

}
